package cn.dfusion.back.controller;

import cn.dfusion.back.service.VaccineService;
import cn.dfusion.blockchain.BlockChainException;
import cn.dfusion.blockchain.entity.BlockInfo;
import cn.dfusion.blockchain.entity.BlockOverviewInfo;
import cn.dfusion.blockchain.entity.Message;
import cn.dfusion.blockchain.entity.TransactionInfo;
import cn.dfusion.blockchain.repo.BlockChainClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ipenta
 * @ClassName: IndexController
 * @Description: TODO
 * @date 2017-09-22 下午12:00
 */

@Controller
public class IndexController {

	private final static Long DEFAULT_PAGE_COUNT = 50L;
	private final static Integer DEFAULT_SORT_TYPE = 1;

	@Autowired
	BlockChainClient blockChainClient;

	@Autowired
	VaccineService vaccineService;

	@GetMapping("/")
	public String index(Map<String, Object> map) {

		List<TransactionInfo> transactionInfoList = new ArrayList<>();

		long blockHeight = messageConverter(blockChainClient.lookupBlockHeight());

		List<BlockOverviewInfo> blockOverviewInfoList = messageConverter(blockChainClient.lookupBlockOverviewTop(5L));

		List<BlockInfo> blockInfoList = messageConverter(blockChainClient.lookupBlockListByHeight(blockHeight - 5, blockHeight));

		for (BlockInfo blockInfo : blockInfoList) {
			transactionInfoList.addAll(blockInfo.getTransactions());
		}

		map.put("blockHeight", blockHeight);
		map.put("blockOverviewInfoList", blockOverviewInfoList);
		map.put("transactionInfoList", transactionInfoList);

		return "index";
	}

	@GetMapping("/chain/overview")
	public String overviewChain(@RequestParam(required = false) Long height,
								@RequestParam(required = false) Long current,
								@RequestParam(required = false) Long pageCount,
								@RequestParam(required = false) Integer sortType,
								Map<String, Object> map) {

		if (null == height) {

			if (null == current) {
				current = 1L;
			}
			if (null == pageCount) {
				pageCount = DEFAULT_PAGE_COUNT;
			}
			if (null == sortType) {
				sortType = DEFAULT_SORT_TYPE;
			}

			List<BlockOverviewInfo> blockOverviewInfoList = messageConverter(blockChainClient.lookupBlockOverviewGroup(current, pageCount, sortType));
			map.put("blockOverviewInfoList", blockOverviewInfoList);
			return "chain/overview";

		}

		BlockOverviewInfo blockOverviewInfo = messageConverter(blockChainClient.lookupBlockOverviewByHeight(height));
		BlockInfo blockInfo = messageConverter(blockChainClient.lookupBlockByHeight(height));
		map.put("blockOverviewInfo", blockOverviewInfo);
		map.put("transactions", blockInfo.getTransactions());

		return "chain/overview";
	}

	@GetMapping("/tx")
	public String overviewTx(@RequestParam String hash,
							 Map<String, Object> map) {
		TransactionInfo transactionInfo  =	messageConverter(blockChainClient.lookupTxByHash(hash));
		map.put("transactionInfo", transactionInfo);

		return "tx/overview";
	}

	@GetMapping("/tx/record")
	public String record(@RequestParam String vaccineId,
						 Map<String, Object> map) {
		map.put("records",vaccineService.findRecord(vaccineId));
		return "tx/record";
	}

	// 数据转换
	private <T> T messageConverter(Message<T> message) {
		if (message.getStatus().equals(Message.Status.SUCCESS.getValue())) {
			return message.getData();
		} else {
			throw new BlockChainException(message.getErrorContent());
		}
	}

}
